package shop.controller.front.order;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.order.OrderService;
import shop.services.front.order.bean.Order;
import shop.services.front.orderdetail.OrderdetailService;
import shop.services.front.orderdetail.bean.Orderdetail;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/18.
 */
@Controller
@RequestMapping("/front/order")
public class OrderController extends FrontBaseController<Order>{
    @Resource
    private OrderService orderService;
    @Resource
    private OrderdetailService orderdetailService;
    @Override
    public Services<Order> getService() {
        return orderService;
    }


    /**
     * 页面跳转方法
     * @return 目标页面
     */
    @RequestMapping("/toOrderList")
    public String toOrderList() {return "/front/order/orderList";}
    @RequestMapping("/toOrderPayList")
    public String toOrderPayList() {return "/front/order/orderPayList";}
    @RequestMapping("/toOrderDetailList")
    public String toOrderDetailList(){ return "/front/order/orderDetail";}
    @RequestMapping ("/toAddAddress")
    public String toAddAddress(){ return "/front/order/addAddress";}
    @RequestMapping("/toManegeAddress")
    public String toManegeAddress(){return "/front/order/manegeAddress";}
    @RequestMapping ("/toEditAddress")
    public String toEditAddress(){return "/front/order/editAddress";}
    @RequestMapping ("/toActivityDetail")
    public String toActivityDetail(){return "/front/activity/activityDetail";}
    @RequestMapping ("/toActivityOrder")
    public String toActivityOrder(){return "/front/activity/activityOrder";}
    /**
     * 查询订单列表
     * @author zhuangjf
     * @param order 订单
     * @return 订单列表
     */
    @RequestMapping(value = "/selectOrder",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectOrder(Order order){
        JSONResult jsonResult=new JSONResult();
        List<Order> list=orderService.selectList(order);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询订单详情列表
     * @return 订单详情列
     */
    @RequestMapping(value ="/selectOrderDetail",method =RequestMethod.GET )
    @ResponseBody
    public JSONResult selectOrderDetail(Orderdetail orderdetail){
        JSONResult rs =new JSONResult();
        Integer orderID=orderdetail.getOrderID();
        Orderdetail orderdetail1=new Orderdetail();
        orderdetail1.setOrderID(orderID);
        rs.setData(orderdetailService.selectDetailList(orderdetail1));
        rs.setSuccess(true);
        return rs;
    }

    /**
     * 创建订单
     * @param order 订单
     * @return 是否成功
     */
    @RequestMapping(value = "/insertOrder",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertOrder(Order order) {
        JSONResult rs =new JSONResult();
        order.setStatus("init");
        int orderID=orderService.insert(order);
        Map rsmap=new HashMap<>();
        rs.setData(rsmap);
        rs.setSuccess(true);
        return rs;
    }

    /**
     * 根据用户ID查询
     * @param order 订单
     * @return 订单列表
     */
    @RequestMapping(value = "/selectOrderByAccount",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectOrderByAccount(Order order){
        JSONResult rs=new JSONResult();
        List<Order> orderList=orderService.selectListByAccountId(order);
        List<Orderdetail> result=new ArrayList<>();
        for(int i=0;i<orderList.size();i++){
            Order order1=orderList.get(i);
            Orderdetail orderdetail=new Orderdetail();
            orderdetail.setOrderID(Integer.parseInt(order1.getId()));
            orderList.get(i).setOrderdetails(orderdetailService.selectDetailList(orderdetail));
        }
        rs.setData(orderList);
        rs.setSuccess(true);
        return rs;
    }

    /**
     * 更新订单
     */
    @RequestMapping(value = "/updateOrder",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateOrder(Order order){
        JSONResult rs=new JSONResult();
        orderService.update(order);
        rs.setSuccess(true);
        return rs;
    }

    /**
     * 取消订单时删除订单
     * @param order 要删除的订单
     */
    @RequestMapping(value = "/deleteOrderById",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteOrderById(Order order){
        JSONResult rs=new JSONResult();
        Orderdetail orderdetail=new Orderdetail();
        orderdetail.setOrderID(Integer.parseInt(order.getId()));
        orderService.delete(order);
        orderdetailService.deleteByOrderId(orderdetail);
        rs.setSuccess(true);
        return rs;
    }
    /**
     * 完成订单
     * @param order 要完成的订单
     */
    @RequestMapping(value = "/finishOrder",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult closeOrder(Order order){
       JSONResult rs =new JSONResult();
        order.setStatus("finish");
        getService().update(order);
        rs.setSuccess(true);
        return rs;
    }
}
